#!/usr/bin/env python
"""
# -*- coding: utf-8 -*-
# @Time    : 2020/9/16 16:16
# @Author  : AshJo
# @Desc    : 遍历文件夹所有的文件和大小
# @Filename: os_dir.py
# @Version : v1.0
"""

import os


def show_files(path):
    """
    os模块遍查看一个文件夹下的所有文件.这个文件夹下面还有文件夹.不能用walk
    :param path:
    :return:
    """
    name_list = os.listdir(path)
    for name in name_list:
        abs_path = os.path.join(path, name)
        if os.path.isfile(abs_path):
            print(name)
        elif os.path.isdir(abs_path):
            show_files(abs_path)


def dir_size(path):
    """
    os模块计算一个文件夹下所有文件的大小.这个文件夹下面还有文件夹.不能用walk
    :param path:
    :return:
    """
    size = 0
    name_list = os.listdir(path)
    for name in name_list:
        abs_path = os.path.join(path, name)
        if os.path.isfile(abs_path):
            size += os.path.getsize(abs_path)
        elif os.path.isdir(abs_path):
            ret = dir_size(abs_path)
            size += ret
    return size


path = r'D:\VScode\Python\Project\Python_fullstack'
show_files(path)
ret = dir_size(path)
print(ret)
